package icraf.carbon.gui.model;

import java.util.ArrayList;

import icraf.carbon.core.CostBenefitUnit;
import icraf.carbon.gui.MatrixTableModel;
import icraf.carbon.gui.NumberRowHeader;
import icraf.carbon.model.ModelProject;

import javax.swing.ListModel;

public class ModelEmissionTableModel extends MatrixTableModel {
	private static final long serialVersionUID = 8968868974903218229L;
	private ModelProject modelProject;
	private String header[] = {"Iteration Period"};
	private ArrayList<CostBenefitUnit> cbuList;

	public ModelProject getModelProject() {
		return modelProject;
	}

	public void setModelProject(ModelProject modelProject) {
		this.modelProject = modelProject;
		if(modelProject != null)
		cbuList = modelProject.getMainProject().getCostBenefitUnitList();
		tableListModel.fireContentChange(getRowCount());
		fireTableDataChanged();
	}

	@Override
	public Class<?> getColumnClass(int column) {
		if (column == 0)
			return Integer.class;
		return Double.class;
	}

	@Override
	public int getColumnCount() {
		if(cbuList == null) return 1;
		return cbuList.size()+1;
	}

	public String getColumnName(int col) {
		if(col == 0)
			return header[col];
		return cbuList.get(col-1).getLabel();
	}

	@Override
	public int getRowCount() {
		return modelProject == null?0:modelProject.getIterationCount()+1;
	}

	@Override
	public Object getValueAt(int row, int column) {
		if (column == 0) {
			return row;
		}

		CostBenefitUnit cbu = cbuList.get(column-1);
		return modelProject.getEmissionThreshold(row,cbu);
	}

	public boolean isCellEditable(int row, int col) {
		return false;
	}

	private NumberRowHeader tableListModel = new NumberRowHeader(this);

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ListModel getRowHeaderModel() {
		return tableListModel;
	}



}
